﻿using PMS.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PMS.Logic.BD
{
    public partial class BottleAjax : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            String methodName = Request["method"];
            Type type = this.GetType();
            MethodInfo method = type.GetMethod(methodName);
            if (method == null) throw new Exception("method is null");
            try
            {

                method.Invoke(this, null);
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 获取气瓶信息
        /// </summary>
        public void GetBottleList()
        {
            List<SearchCondition> listSearch = new List<SearchCondition>();
            if (Request.Form["searchData"] != null)
            {
                string searchData = Request.Form["searchData"];
                listSearch = ModelTools.GetSearchContions(searchData);
                if (listSearch.Where(s => s.Field == "startPrdDate").FirstOrDefault() != null)
                {
                    listSearch.Where(s => s.Field == "startPrdDate").FirstOrDefault().Field = "ProductionDate";
                }
                if (listSearch.Where(s => s.Field == "endPrdDate").FirstOrDefault() != null)
                {
                    listSearch.Where(s => s.Field == "endPrdDate").FirstOrDefault().Field = "ProductionDate";
                }
                if (listSearch.Where(s => s.Field == "startChkDate").FirstOrDefault() != null)
                {
                    listSearch.Where(s => s.Field == "startChkDate").FirstOrDefault().Field = "LastCheckDate";
                }
                if (listSearch.Where(s => s.Field == "endChkDate").FirstOrDefault() != null)
                {
                    listSearch.Where(s => s.Field == "endChkDate").FirstOrDefault().Field = "LastCheckDate";
                }
            }
            Expression<Func<View_BD_BottleInfo, bool>> exp = SearchExpression.FindByGroup<View_BD_BottleInfo>(listSearch);
            string OrderId = OperateContext.Sort == null ? "ID" : OperateContext.Sort;
            int count = 0;
            var list = DALSession.Current.View_BD_BottleInfoDAL.GetPagedList(OperateContext.Page, OperateContext.Rows, exp, OrderId, OperateContext.Order);
            count = DALSession.Current.View_BD_BottleInfoDAL.GetCount(exp);
            Response.Write(JsonHelper.toPageJson(list, count));
        }

        /// <summary>
        /// 根据q获取气瓶信息
        /// </summary>
        public void GetBottleInfoByQ()
        {
            string q = Request.Form["q"];
            var a = new List<BD_BottleInfo>();
            List<SearchCondition> listSearch = new List<SearchCondition>();
            Expression<Func<BD_BottleInfo, bool>> exp = SearchExpression.FindByGroup<BD_BottleInfo>(listSearch);
            string OrderId = OperateContext.Sort == null ? "ID" : OperateContext.Sort;
            int count = 0;
            if (q == null)
            {
                var list = DALSession.Current.BD_BottleInfoDAL.GetPagedList(OperateContext.Page, OperateContext.Rows, exp, OrderId, false);
                count = DALSession.Current.BD_BottleInfoDAL.GetCount(exp);
                Response.Write(JsonHelper.toPageJson(list, count));
            }
            else
            {
                SearchCondition search = new SearchCondition();
                search.Field = "BottleCode";
                search.Value = q;
                search.Operator = "co";
                listSearch.Add(search);
                exp = SearchExpression.FindByGroup<BD_BottleInfo>(listSearch);
                var list = DALSession.Current.BD_BottleInfoDAL.GetPagedList(OperateContext.Page, OperateContext.Rows, exp, OrderId, false);
                count = DALSession.Current.BD_BottleInfoDAL.GetCount(exp);
                Response.Write(JsonHelper.toPageJson(list, count));
            }
        }
        /// <summary>
        /// 修改气瓶信息
        /// </summary>
        public void EditBottle()
        {
            int ID = Commons.String2Int32(Request.Form["ID"]);
            BD_BottleInfo perNew = DALSession.Current.BD_BottleInfoDAL.GetListBy(d => d.ID == ID).FirstOrDefault();
            ModelTools.Form2EditModel(Request.Form, perNew);
            DALSession.Current.BD_BottleInfoDAL.ModifyAllModel(perNew);
            DALSession.Current.BD_BottleInfoDAL.SaveChanges();
            Response.Write(JsonHelper.toDefaultMsg(true, "保存成功"));
        }

        /// <summary>
        /// 添加气瓶信息
        /// </summary>
        public void AddBottle()
        {
            BD_BottleInfo per = new BD_BottleInfo();
            ModelTools.Form2EditModel(Request.Form, per);
            DALSession.Current.BD_BottleInfoDAL.Add(per);
            DALSession.Current.BD_BottleInfoDAL.SaveChanges();
            Response.Write(JsonHelper.toDefaultMsg(true, "保存成功"));
        }

        /// <summary>
        /// 删除气瓶信息
        /// </summary>
        public void DelBottle()
        {
            //  int Id = Commons.String2Int32(Request.Form["Id"]);
            if (Request.Form["ID"] == null)
            {
                Response.Write(JsonHelper.toDefaultMsg(false, "删除失败"));
                return;
            }
            int Id = Convert.ToInt32(Request.Form["ID"].ToString());
            DALSession.Current.BD_BottleInfoDAL.DelBy(p => p.ID == Id);
            DALSession.Current.BD_BottleInfoDAL.SaveChanges();
            Response.Write(JsonHelper.toDefaultMsg(true, "删除成功"));
        }

        public void BottleList2Excel()
        {
            //1.首先获取要导出的实体数据集合list
            List<SearchCondition> listSearch = new List<SearchCondition>();
            if (Request.Form["searchData"] != null)
            {
                string searchData = Request.Form["searchData"];
                listSearch = ModelTools.GetSearchContions(searchData);
                if (listSearch.Where(s => s.Field == "startPrdDate").FirstOrDefault() != null)
                {
                    listSearch.Where(s => s.Field == "startPrdDate").FirstOrDefault().Field = "ProductionDate";
                }
                if (listSearch.Where(s => s.Field == "endPrdDate").FirstOrDefault() != null)
                {
                    listSearch.Where(s => s.Field == "endPrdDate").FirstOrDefault().Field = "ProductionDate";
                }
                if (listSearch.Where(s => s.Field == "startChkDate").FirstOrDefault() != null)
                {
                    listSearch.Where(s => s.Field == "startChkDate").FirstOrDefault().Field = "LastCheckDate";
                }
                if (listSearch.Where(s => s.Field == "endChkDate").FirstOrDefault() != null)
                {
                    listSearch.Where(s => s.Field == "endChkDate").FirstOrDefault().Field = "LastCheckDate";
                }
            }
            Expression<Func<View_BD_BottleInfo, bool>> exp = SearchExpression.FindByGroup<View_BD_BottleInfo>(listSearch);
            string OrderId = OperateContext.Sort == null ? "ID" : OperateContext.Sort;
            var list = DALSession.Current.View_BD_BottleInfoDAL.GetListBy(exp).OrderBy(p => p.ID).ToList();
            // 2.设置单元格抬头
            // key：实体对象属性名称，可通过反射获取值
            // value：Excel列的名称
            string cellhead = Request.Form["cellhead"];
            JavaScriptSerializer serial = new JavaScriptSerializer();
            Dictionary<string, string> JsonData = serial.Deserialize<Dictionary<string, string>>(cellhead);
            // 3.进行Excel转换操作，并返回转换的文件下载链接
            string urlPath = ExcelHelper.EntityListToExcel2003(JsonData, list, "气瓶检查记录表");
            Response.Write(JsonHelper.toJson(urlPath)); // 返回Json格式的内容
        }

    }
}